package com.littleHan.brushQuestions.web.frontPage.service.impl;

import com.littleHan.brushQuestions.exception.FailedSettingException;
import com.littleHan.brushQuestions.exception.FailedToDeleteException;
import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.StringUtil;
import com.littleHan.brushQuestions.web.frontPage.dao.FrontPageDao;
import com.littleHan.brushQuestions.web.frontPage.pojo.Interesting;
import com.littleHan.brushQuestions.web.frontPage.service.FrontPageService;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import com.littleHan.brushQuestions.web.start.pojo.vo.TopicVo;
import com.littleHan.brushQuestions.web.userCenter.pojo.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FrontPageServiceImpl implements FrontPageService {

    @Autowired
    private FrontPageDao frontPageDao;

    @Override
    public List<TopicVo> getHotTopicList(String userId) throws FailedToLoadException {
        //根据用户Id获取用户的兴趣(学科)
        List<Interesting> interestingList=frontPageDao.getInterestingByUserId(userId);
        if(interestingList.isEmpty()){
            //如果兴趣为空说明用户还没有选择兴趣，那么就先获取所有题目
            List<TopicVo> topicVoList=frontPageDao.getTopic();
            //根据题号获取其收藏数量和评论数量
            for(TopicVo t:topicVoList){
                int collectNum=frontPageDao.getCollectNumByTopicId(t.getId());
                t.setCollectNum(collectNum);
                int commentNum=frontPageDao.getCommentNumByTopicId(t.getId());
                t.setCommentNum(commentNum);
                int userIsCollect=frontPageDao.userIsCollect(t.getId(),userId);
                if(userIsCollect>0){
                    t.setUserIsCollect(true);
                }
                else {
                    t.setUserIsCollect(false);
                }
            }
            List<TopicVo> newList = topicVoList.subList(0, 5);

            if(newList.isEmpty()){
                throw new FailedToLoadException("获取热门题目失败");
            }
            //排序从大到小，TopicVo重写compareTo方法
            Collections.sort(newList);
            return newList;
        }
        //用户选择了兴趣情况
        //根据objectId获取题目列表前5条
       List<TopicVo> topicList=frontPageDao.getTopicByObjectId(interestingList);
       //根据题号获取其收藏数量和评论数量
        for(TopicVo t:topicList){
            int collectNum=frontPageDao.getCollectNumByTopicId(t.getId());
            t.setCollectNum(collectNum);
            int commentNum=frontPageDao.getCommentNumByTopicId(t.getId());
            t.setCommentNum(commentNum);
            int userIsCollect=frontPageDao.userIsCollect(t.getId(),userId);
            if(userIsCollect>0){
                t.setUserIsCollect(true);
            }
            else {
                t.setUserIsCollect(false);
            }
        }
        if(topicList.isEmpty()){
            throw new FailedToLoadException("获取热门题目失败");
        }
        //排序从大到小，TopicVo重写compareTo方法
        Collections.sort(topicList);
        return topicList;
    }


    @Override
    public List<TopicVo> getNewTopicList(String userId) throws FailedToLoadException {
        //根据用户Id获取用户的兴趣(学科)
        List<Interesting> interestingList=frontPageDao.getInterestingByUserId(userId);
        if(interestingList.isEmpty()){
            //如果兴趣为空说明用户还没有选择兴趣，那么就先获取5个题目
            List<TopicVo> topicVoList=frontPageDao.getTopicSortByTime();
            //根据题号获取其收藏数量和评论数量以及用户是否收藏过
            for(TopicVo t:topicVoList){
                int collectNum=frontPageDao.getCollectNumByTopicId(t.getId());
                t.setCollectNum(collectNum);
                int commentNum=frontPageDao.getCommentNumByTopicId(t.getId());
                t.setCommentNum(commentNum);
                int userIsCollect=frontPageDao.userIsCollect(t.getId(),userId);
                if(userIsCollect>0){
                    t.setUserIsCollect(true);
                }
                else {
                    t.setUserIsCollect(false);
                }
            }
            if(topicVoList.isEmpty()){
                throw new FailedToLoadException("获取最新题目失败");
            }
            return topicVoList;
        }
        //用户选择了兴趣情况
        List<TopicVo> topicList=frontPageDao.getTopicByObjectIdAndSortByTime(interestingList);
        //根据题号获取其收藏数量和评论数量以及用户是否收藏过
        for(TopicVo t:topicList){
            int collectNum=frontPageDao.getCollectNumByTopicId(t.getId());
            t.setCollectNum(collectNum);
            int commentNum=frontPageDao.getCommentNumByTopicId(t.getId());
            t.setCommentNum(commentNum);
            int userIsCollect=frontPageDao.userIsCollect(t.getId(),userId);
            if(userIsCollect>0){
                t.setUserIsCollect(true);
            }
            else {
                t.setUserIsCollect(false);
            }
        }
        if(topicList.isEmpty()){
            throw new FailedToLoadException("获取热门题目失败");
        }
        return topicList;
    }

    @Override
    public List<DicValue> getInterestingSpecificInfoByUserId(String userId) {
        List<DicValue> dicValueList=frontPageDao.getInterestingSpecificInfoByUserId(userId);
        return dicValueList;
    }

    @Override
    public List<DicValue> getAllObject() throws FailedToLoadException {
        List<DicValue> dicValueList=frontPageDao.getAllObject();
        if (dicValueList.isEmpty()){
            throw new FailedToLoadException("获取兴趣列表失败");
        }
        return dicValueList;
    }

    @Override
    public void setInterestingByUser(List<DicValue> list, String userId) throws FailedSettingException {
        //查询用户是否选择过兴趣
        int selectResult=frontPageDao.selectInterestingByUserId(userId);
        if(selectResult>=1){
            //重置用户的兴趣(实质就是删除)
            int deleteResult=frontPageDao.deleteUserInteresting(userId);
            if(deleteResult<1){
                throw new FailedToDeleteException("删除兴趣失败");
            }
        }
        //重写设置其兴趣
        for(DicValue d:list){
            String id= StringUtil.generateUUID();
            int setResult=frontPageDao.setInterestingByUser(id,userId,d.getId());
            if(setResult<1){
                throw new FailedToInsertException("修改兴趣失败");
            }
        }
    }

    @Override
    public List<Announcement> getAnnouncementList() {
        List<Announcement> list=frontPageDao.getAnnouncementList();
        return list;
    }
}
